package com.miracle9.game.dao;

import com.miracle9.game.entity.SystemConfig;

import java.io.PrintStream;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Repository;

@Repository
public class SystemConfigServiceDao extends SqlOperating<SystemConfig, Integer> {

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public Integer updateSystemConfigAll(SystemConfig config) {
        String hql = "update SystemConfig set userCheck=:userCheck,payCheckMoney=:payCheckMoney,expiryCheckMoney=:expiryCheckMoney,leaseCheck=:leaseCheck,authorize=:authorize,sumPayMoney=:sumPayMoney,sumExpiryMoney=:sumExpiryMoney,userSumMoney=:userSumMoney,moneyOverrun=:moneyOverrun,notActive=:notActive,chat=:chat,promoterSumMoney=:promoterSumMoney,registVerify=:registVerify,updateAddress=:updateAddress,robotPromoter=:robotPromoter,robotUpdateAddress=:robotUpdateAddress,robotGold=:robotGold,robotAutoGold=:robotAutoGold,shareAddress=:shareAddress,serviceUrl=:serviceUrl,openLuckGame=:openLuckGame,openFishGame=:openFishGame,openCardGame=:openCardGame,openBulletGame=:openBulletGame,openMermaidGame=:openMermaidGame,openWaterGame=:openWaterGame,openLiKuiGame=:openLiKuiGame,openToadGame=:openToadGame,openBcbmGame=:openBcbmGame,openDntgGame=:openDntgGame,openSharkGame=:openSharkGame,openDragonTigerGame=:openDragonTigerGame,rebate1=:rebate1,rebate2=:rebate2,rebate3=:rebate3,rechargeRebate=:rechargeRebate,randomAmount=:randomAmount";
        Query query = getSession().createQuery(hql);
        query.setProperties(config);
        return query.executeUpdate();
    }

    public SystemConfig getSystemConfig() {
        return q_CreateSqlFilter("from SystemConfig", new Object[0]);
    }

    public SystemConfig getSystemConfigUniqueResult() {
        SystemConfig sc = (SystemConfig) q_CreateSql("from SystemConfig", new Object[0]).uniqueResult();
        System.out.println("++++++++++++" + sc.getOpenWaterGame());
        return sc;
    }

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public void updateSystemConfigInfo_5(int payScale, int promoterPayScale, int switchType, int interactPassword, int expiry) {
        q_CreateSql("update SystemConfig set payScale=?,promoterPayScale=?,switchType=?,interactPassword=?,expiry=?", new Object[]{Integer.valueOf(payScale), Integer.valueOf(promoterPayScale), Integer.valueOf(switchType), Integer.valueOf(interactPassword), Integer.valueOf(expiry)}).executeUpdate();
    }

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public void updateAlltemp(SystemConfig config) {
        String hql = "update SystemConfig set tempSumPayMoney=?,tempSumExpiryMoney=?,tempUserSumMoney=?,tempPromoterSumMoney=?";
        q_CreateSql(hql, new Object[]{Integer.valueOf(config.getTempSumPayMoney()), Integer.valueOf(config.getTempSumExpiryMoney()), Integer.valueOf(config.getTempUserSumMoney()),
                Integer.valueOf(config.getTempPromoterSumMoney())}).executeUpdate();
    }

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public void resetTempConfig() {
        String hql = "update SystemConfig set tempSumPayMoney=0,tempSumExpiryMoney=0,tempUserSumMoney=0,tempPromoterSumMoney=0";
        q_CreateSql(hql, new Object[0]).executeUpdate();
    }

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public void updateWeihuStatus(SystemConfig config) {
        String hql = "update SystemConfig set weihuTime=?,content=?,gameStatus=?,operationStatus=?";
        q_CreateSql(hql, new Object[]{Integer.valueOf(config.getWeihuTime()), config.getContent(), Integer.valueOf(config.getGameStatus()),
                Integer.valueOf(config.getOperationStatus())}).executeUpdate();
    }

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public void updateOperation_3(int status, String startDate, long endTime) {
        String hql = "update SystemConfig set operationStatus=?,operationDate=?,operationStopDate=?";
        q_CreateSql(hql, new Object[]{Integer.valueOf(status), startDate, Long.valueOf(endTime)}).executeUpdate();
    }

    @CacheEvict(value = {"springCache"}, key = "'getSystemConfig'")
    public void ifTypeUpdate(int type, int status) {
        String hql = null;
        if (type == 0) {
            hql = "update SystemConfig set openLuckGame=?";
        } else if (type == 1) {
            hql = "update SystemConfig set openFishGame=?";
        } else if (type == 2) {
            hql = "update SystemConfig set openCardGame=?";
        } else if (type == 3) {
            hql = "update SystemConfig set openBulletGame=?";
        } else if (type == 4) {
            hql = "update SystemConfig set openMermaidGame=?";
        } else if (type == 5) {
            hql = "update SystemConfig set openLackGame=?";
        } else if (type == 6) {
            hql = "update SystemConfig set openJoyGame=?";
        } else if (type == 7) {
            hql = "update SystemConfig set openWaterGame=?";
        }
        int count = q_CreateSql(hql, new Object[]{Integer.valueOf(status)}).executeUpdate();

        System.out.println("执行结果  " + count);
    }

    public void updateBaodanPwd(String newPwd) {
        q_CreateSql("update SystemConfig set baodanPwd = ?", new Object[]{newPwd}).executeUpdate();
    }

    public void updateBaodanStatus(int status) {
        q_CreateSql("update SystemConfig set baodanStatus = ?", new Object[]{Integer.valueOf(status)}).executeUpdate();
    }

    public void updateLackBaodanStatus(int status) {
        q_CreateSql("update SystemConfig set lackBaodanStatus = ?", new Object[]{Integer.valueOf(status)}).executeUpdate();
    }

}
